package com.yinhai.message.implement.dwr.sender;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yinhai.message.api.bean.TMMessage;
import com.yinhai.message.api.bean.TMUser;
import com.yinhai.message.api.vo.TMMessageVo;
import com.yinhai.message.core.po.MessageFilePo;
import com.yinhai.message.core.sender.IMessageSender;
import com.yinhai.message.core.util.DateFormatUtil;
import com.yinhai.message.implement.dwr.util.SendMessageAutoUtil;
import com.yinhai.message.search.bpo.IMessageSearchBpo;

/**
 * @author yanglq
 *         2017.1.9
 */
@Service
public class DwrMessageSender implements IMessageSender<TMMessageVo>{
	
	@Resource
	private IMessageSearchBpo messageSearchBpo;
	
    @Override
    public List<TMUser> sendMessage(TMMessageVo messageSendVO) {

        TMMessage tmmsg = messageSendVO.getMessage();
        TMUser sendUser = messageSendVO.getUser();
        
        List<MessageFilePo> listFilePo = messageSearchBpo.getMessageFile(tmmsg.getMgid());
        StringBuilder fileStr = new StringBuilder("附件：").append("<br>");
		for(MessageFilePo filePo:listFilePo){
			fileStr.append(filePo.getFilename())
				   .append(":<a id='")
				   .append(filePo.getId())
				   .append("' href='javascript:mgAtmDownload(")
				   .append(filePo.getId())
				   .append(")' style='color: #1A5E9E;cursor: pointer'>下载</a>")
				   .append("<br>");
		}
		
        Map<String, String> msg = new HashMap<>();
        msg.put("mgid", tmmsg.getMgid());
        msg.put("title", tmmsg.getTitle());
        msg.put("content", tmmsg.getContent());
        msg.put("attachment", fileStr.toString());
        msg.put("mgdateShow", DateFormatUtil.df2.format(tmmsg.getCreateDate()));
        msg.put("suserid", sendUser.getUserId());
        msg.put("sname", sendUser.getName());
        msg.put("read", "0");
        msg.put("selfType", tmmsg.getSelfType());
		msg.put("otherData1", tmmsg.getOtherData1());
		msg.put("otherData2", tmmsg.getOtherData2());
		msg.put("otherData3", tmmsg.getOtherData3());
        SendMessageAutoUtil.sendMessageAuto(messageSendVO.getUserlist(), msg, tmmsg.getScriptMethod());
        return messageSendVO.getUserlist();
    }

}
